<?php
 class ChapterController extends UserController
 {
     public function init()
     {
         parent::init();
         if ($this->userinfo['author'] != 1) {
             $this->error('您尚未申请作者或者作者申请尚未通过审核');
         }
         $this->assign('userpagesign', 'author');
     }
     public function indexAction()
     {
         $zym_10 = $this->input->get('id');
         $this->assign('info', $this->model('novelsearch_info')->getinfo($zym_10));
         $this->display('chapter');
     }
     public function editAction()
     {
         $zym_10 = $this->input->get('id');
         $zym_9 = $this->input->get('cid');
         $zym_11 = $this->model('novelsearch_info')->getinfo($zym_10);
         $zym_8 = $this->model('novelsearch_chapter')->getinfo($zym_10, $zym_9, $zym_11, false);
         if ($this->request->isPost()) {
             $zym_12 = novel::chapterformat($this->input->post('content', 'str', ''));
             $zym_5 = [ 'id' => $zym_9, 'novelid' => $zym_10, 'name' => novel::nameformat($this->input->post('name', 'str', '')), 'url' => mb_strlen($zym_12), 'time' => NOW_TIME, ];
             if ($this->model('novelsearch_chapter')->edit($zym_5)) {
                 F(DATA_PATH . '/chapter/' . subid($zym_10) . '/' . $zym_10 . '/' . $zym_9 . '.txt', $zym_12);
                 $zym_6 = [ 'id' => $zym_10, 'lastupdate' => NOW_TIME, ];
                 if ($zym_8['oid'] == $zym_11['last']['sign']) {
                     $zym_6['lastchaptername'] = $zym_5['name'];
                 }
                 $this->model('novelsearch_info')->edit($zym_6);
                 $this->success('发布章节成功', U('index', ['id' => $zym_10]));
             } else {
                 $this->error('发布新章节失败');
             }
         }
         $zym_8['content'] = str_replace('<br/><br/>　　', "\n", $zym_8['content']);
         $zym_8['content'] = preg_replace('/^　　/', "", $zym_8['content']);
         $this->assign('info', $zym_11);
         $this->assign('chapter', $zym_8);
         $this->display('chapteredit');
     }
     public function addAction()
     {
         $zym_10 = $this->input->get('id');
         $zym_11 = $this->model('novelsearch_info')->getinfo($zym_10);
         if ($this->request->isPost()) {
             $zym_12 = novel::chapterformat($this->input->post('content', 'str', ''));
             $zym_5 = [ 'name' => novel::nameformat($this->input->post('name', 'str', '')), 'oid' => $zym_11['last']['sign'] + 1, 'novelid' => $zym_11['novel']['id'], 'siteid' => 0, 'url' => mb_strlen($zym_12), 'time' => NOW_TIME, ];
             if ($zym_7 = $this->model('novelsearch_chapter')->add($zym_5)) {
                 F(DATA_PATH . '/chapter/' . subid($zym_10) . '/' . $zym_10 . '/' . $zym_7 . '.txt', $zym_12);
                 $zym_6 = [ 'id' => $zym_10, 'lastchapterid' => $zym_7, 'lastchaptername' => $zym_5['name'], 'lastsiteid' => 0, 'lastupdate' => NOW_TIME, 'chaptersign' => $zym_5['oid'], ];
                 $this->model('novelsearch_info')->edit($zym_6);
                 $this->success('发布章节成功', U('index', ['id' => $zym_10]));
             } else {
                 $this->error('发布新章节失败');
             }
         }
         $this->assign('info', $zym_11);
         $this->display('chapteradd');
     }
 }
